package ee.android.kinokavad.fragments;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;

import ee.android.kinokavad.R;
import ee.android.kinokavad.activities.KinoKavadActivity;
import ee.android.kinokavad.adapters.NavigationAdapter;
import ee.android.kinokavad.data.DrawerItem;

public class DrawerFragment extends SherlockFragment implements OnClickListener, OnItemClickListener{

	public static final int MAIN_FRAGMENT = 0;
	public static final int MOVIES_FRAGMENT = 1;
	public static final int CINEMAS_FRAGMENT = 2;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.drawer_fragment, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		initViews(savedInstanceState);
	}

	private void initViews(Bundle savedInstanceState) {

		List<DrawerItem> navItems = new ArrayList<DrawerItem>();
		navItems.add(new DrawerItem(
				MainFragmentKinoKavad.class.getSimpleName(), "SEANSID", true, MAIN_FRAGMENT));
		navItems.add(new DrawerItem(
				MoviesFragment.class.getSimpleName(), "FILMID", false, MOVIES_FRAGMENT));
		navItems.add(new DrawerItem(
				CinemasFragment.class.getSimpleName(), "KINOD", false, CINEMAS_FRAGMENT));

		ListView list = (ListView) getView().findViewById(R.id.drawerList);
		
		View footer = LayoutInflater.from(getActivity()).inflate(R.layout.drawer_fragment_footer, null);
		list.addFooterView(footer, null, false);
		NavigationAdapter adapter = new NavigationAdapter(getActivity(), navItems);
		list.setAdapter(adapter);
		list.setOnItemClickListener(this);
		
		getView().findViewById(R.id.drawerFeedback).setOnClickListener(this);
		getView().findViewById(R.id.drawerKavadLogo).setOnClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

		DrawerItem fragment = (DrawerItem) parent.getItemAtPosition(position);
		switch (fragment.getFragment()) {
		case MAIN_FRAGMENT:
			((KinoKavadActivity) getActivity()).handleSessionsClick(true);
			break;
		case CINEMAS_FRAGMENT:
			((KinoKavadActivity) getActivity()).handleCinemasClick(true);
			break;
		case MOVIES_FRAGMENT:
			((KinoKavadActivity) getActivity()).handleMoviesClick(true);
			break;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.drawerFeedback:
			createFeedbackDialog();
			break;
		case R.id.drawerKavadLogo:
			openKavadHomePage();
			break;


		default:
			break;
		}
	}

	private void openKavadHomePage() {
		Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.kavad.ee"));
		startActivity(browserIntent);
	}

	private void createFeedbackDialog() {
		Intent i = new Intent(Intent.ACTION_SEND);
		i.setType("message/rfc822");
		i.putExtra(Intent.EXTRA_EMAIL, new String[]{"mtiidla@ut.ee"});
		i.putExtra(Intent.EXTRA_SUBJECT, "KinoKavad tagasiside");
		i.putExtra(Intent.EXTRA_TEXT, "");
		try {
			startActivity(Intent.createChooser(i, "Saada email..."));
		} catch (android.content.ActivityNotFoundException ex) {
			Toast.makeText(getActivity(), "Ühtegi emaili klienti pole installitud.", Toast.LENGTH_SHORT).show();
		}
	}


}
